package znet

import (
	"fmt"
	"net"
)

type Server struct {
	Name       string
	Ip         string
	Port       int
	TcpVersion string
}

func (s *Server) Start() {
	server, err := net.Listen(s.TcpVersion, fmt.Sprintf("%s:%s", s.Ip, s.Port))
	if err != nil {
		fmt.Println("listen tcp server error：" + err.Error())
		return
	}
	defer server.Close()
	// 接收客户端请求
	for {
		conn, err := server.Accept()
		if err != nil {
			fmt.Println("get connect error：" + err.Error())
			continue
		}
		go func() {
			for {
				buffer := make([]byte, 512)
				cnt, err := conn.Read(buffer)
				if err != nil {
					fmt.Println("read connect content error：", err.Error())
				}
				fmt.Println(cnt, buffer)
				_, err = conn.Write(buffer[:cnt])
				if err != nil {
					fmt.Println("write data error：" + err.Error())
				}
			}
		}()
	}
}

func (s *Server) Stop() {
	fmt.Println("stop tcp_server")
}
